//
//  ViewController.m
//  JKKVC
//
//  Created by 王冲 on 2018/6/15.
//  Copyright © 2018年 JK科技有限公司. All rights reserved.
//

#import "ViewController.h"
#import "JKPerson.h"
#import "JKDog.h"
#import "JKObserve.h"
#import "JKUser.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   [self valueForKey];
    
}

#pragma mark valueForKey:的原理
-(void)valueForKey{
    
    JKPerson *user = [[JKPerson alloc]init];
    user->_height = 10;
    
    NSLog(@"取出的值==%@", [user valueForKey:@"height"]);
    
}

#pragma mark setValue:forKey:的原理
-(void)setValue{
    
    JKObserve *jkObserve = [[JKObserve alloc]init];
    
    JKPerson *person = [[JKPerson alloc]init];
    
    [person addObserver:jkObserve forKeyPath:@"height" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew  context:NULL];
    [person setValue:@12 forKey:@"height"];
    
    /*
      [person willChangeValueForKey:@"height"]; // 将要改变height的值
      person -> _height; // 赋值
      [person didChangeValueForKey:@"height"];  // 调用KVO的监听方法
     
     */
    
    NSLog(@"height == %@",[person valueForKey:@"height"]);
    
    [person removeObserver:jkObserve forKeyPath:@"height"];
}

#pragma mark KVC的简单使用
-(void)jkKVC1{
    
    /*
    JKPerson *person = [[JKPerson alloc]init];
    
    [person setValue:@12 forKey:@"height"];
    
    person.dog = [[JKDog alloc]init];
    [person setValue:@20 forKeyPath:@"dog.age"];
    
    NSLog(@"height的值 ==== %@  dog.age ==== %@",[person valueForKey:@"height"],[person valueForKeyPath:@"dog.age"]);
     */
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
